package com.linwen.javase.juc;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @ClassName _16WriteReadLockThread
 * @Description
 * @Author lin
 * @Date 2022/4/24 上午12:07
 * @Version V1.0
 *
 * 锁降级
 * 写锁可以降级为读锁
 * 读锁不能升级为写锁
 */
public class _16WriteReadLockThread {
    public static void main(String[] args) {
        ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();
        ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();

        // 先获取写锁
        writeLock.lock();
        System.out.println("获取写锁");
        // 后获取读锁
        readLock.lock();
        System.out.println("获取读锁");
        readLock.unlock();
        writeLock.unlock();



        // 先获取读锁
        readLock.lock();
        System.out.println("获取读锁");
        // 在不释放读锁之前，写锁将不能获取
        // readLock.unlock();
        // 后获取写锁
        writeLock.lock();
        System.out.println("获取写锁");


        writeLock.unlock();

    }
}
